package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.inmail.core.application.command.DingDeptUserSaveCommand;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.DingDeptUser;
import com.smedi.ismedi.inmail.core.domain.repository.DingDeptUserRepository;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.DingDeptUserEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.DingDeptUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: DingDeptUserRepositoryImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/9/20 10:27
 */

@Repository
@RequiredArgsConstructor
public class DingDeptUserRepositoryImpl extends ServiceImpl<DingDeptUserMapper, DingDeptUserEntity> implements DingDeptUserRepository, IService<DingDeptUserEntity> {

    private final AdapterConverter adapterConverter;

    @Override
    public void batchDelete(List<String> userIds, String deptKey) {
        this.baseMapper.batchDelete(userIds, deptKey);
    }

    @Override
    public void batchSave(List<String> userIds, String deptKey) {
        this.baseMapper.batchSave(userIds, deptKey);
    }

    @Override
    public List<String> getUserIdsByDeptKey(String deptKey) {
        return this.baseMapper.getUserIdsByDeptKey(deptKey);
    }

    @Override
    public void batchSaveImlUserId(List<DingDeptUserSaveCommand> dingDeptUserSaveCommands) {
        List<DingDeptUserEntity> dingDeptUserEntities = dingDeptUserSaveCommands.stream().map(adapterConverter::dingDeptUserSaveCommandToDingDeptUserEntity).collect(Collectors.toList());
        this.baseMapper.batchSaveIsmediUserId(dingDeptUserEntities);
    }

    @Override
    public List<DingDeptUser> getListByDeptKey(String deptKey) {
        List<DingDeptUserEntity> dingDeptUserEntities = this.baseMapper.getListByDeptKey(deptKey);
        return adapterConverter.dingDeptUserEntityToDingDeptUser(dingDeptUserEntities);
    }

    @Override
    public List<DingDeptUser> getAllUserIdAndDeptKey() {
        List<DingDeptUserEntity> list = this.baseMapper.getAllUserIdAndDeptKey();
        return adapterConverter.dingDeptUserEntityToDingDeptUser(list);
    }

    @Override
    public List<String> getUserIds() {
        return this.baseMapper.getUserIds();
    }

    @Override
    public Boolean personExist(Long personId) {
        Long count = this.baseMapper.getPersonNum(personId);
        return count != null && count > 0;
    }

    @Override
    public List<String> peopleByProjectId(String projectId) {
        return this.baseMapper.peopleByProjectId(projectId);
    }
}